#!/bin/bash
################################
#RabbitMQ Cluster群集安装配置
#rabbitmqctl手工配置集群，Disk node，镜像模式
#centos7

#host设置

#RabbitMQ Cluster 安装群集
yum install rabbitmq-server erlang socat -y
#systemctl enable rabbitmq-server.service
# echo "
# NODE_PORT=5672
# NODE_IP_ADDRESS=0.0.0.0">/etc/rabbitmq/rabbitmq-env.conf #指定端口
systemctl start rabbitmq-server.service
rabbitmq-plugins enable rabbitmq_management  #启动web插件端口15672
netstat -antp|egrep '567'

#controller1节点
#拷贝到其它节点，统一认证的Erlang Cookie
scp /var/lib/rabbitmq/.erlang.cookie controller2:/var/lib/rabbitmq/
scp /var/lib/rabbitmq/.erlang.cookie controller3:/var/lib/rabbitmq/
#rabbitmqctl status
#使用Disk模式
systemctl stop rabbitmq-server.service
pkill beam.smp
rabbitmqctl stop
rabbitmq-server -detached 
rabbitmqctl cluster_status #查看状态

#controller2、controller3执行加入节点controller1
systemctl stop rabbitmq-server.service
pkill beam.smp
rabbitmq-server -detached 
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@server1
rabbitmqctl start_app
rabbitmqctl cluster_status
#

#任意一个节点上执行
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'  #设置镜像队列
rabbitmqctl set_cluster_name RabbitMQ-Cluster #更改群集名称
rabbitmqctl cluster_status #查看群集状态


#成功
[root@controller1 ~]# rabbitmqctl cluster_status #查看群集状态
Cluster status of node rabbit@controller1 ...
[{nodes,[{disc,[rabbit@controller1,rabbit@controller2,rabbit@controller3]}]},
 {running_nodes,[rabbit@controller3,rabbit@controller2,rabbit@controller1]},
' {cluster_name,<<"RabbitMQ-Cluster">>},'
 {partitions,[]},
 {alarms,[{rabbit@controller3,[]},
          {rabbit@controller2,[]},
          {rabbit@controller1,[]}]}]


#重置: rabbitmqctl stop_app ; rabbitmqctl reset
#rabbitmq-plugins list  #查看支持的插件
#systemctl restart rabbitmq-server.service

#参考 disable
# http://blog.csdn.net/happyzwh/article/details/53008803
# http://blog.csdn.net/woogeyu/article/details/51119101
# http://blog.csdn.net/xuyaqun/article/details/50957254
# http://www.cnblogs.com/wangiqngpei557/p/6158094.html

################################
#以下为openstack高可用相关的配置

#haproxy设置……所有controller节点
echo '#RabbitMQ
listen RabbitMQ-Server
bind controller:5673
mode tcp
balance roundrobin
option tcpka
timeout client  3h
timeout server  3h
option          clitcpka
server controller1 controller1:5672 check inter 5s rise 2 fall 3
server controller2 controller2:5672 check inter 5s rise 2 fall 3
server controller3 controller3:5672 check inter 5s rise 2 fall 3

listen RabbitMQ-Web
bind controller:15673
mode tcp
balance roundrobin
option tcpka
server controller1 controller1:15672 check inter 5s rise 2 fall 3
server controller2 controller2:15672 check inter 5s rise 2 fall 3
server controller3 controller3:15672 check inter 5s rise 2 fall 3
'>>/etc/haproxy/haproxy.cfg
systemctl restart haproxy.service
netstat -antp|grep 567

#访问RabbitMQ,访问地址是http://ip:15672

#其中一节点，添加用户及密码
rabbitmqctl  add_user admin admin
rabbitmqctl  set_user_tags admin administrator
rabbitmqctl add_user openstack openstack 
rabbitmqctl set_permissions openstack ".*" ".*" ".*" 
rabbitmqctl  set_user_tags openstack administrator
systemctl restart rabbitmq-server.service
netstat -antp|grep '5672'